package mulegame;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.ComponentOrientation;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.border.LineBorder;
import java.awt.Dimension;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

/**
 * Creates the starting screen with buttons and action listeners for drop down
 * menus. Players choose their info in the drop down menu.
 * 
 * @author AREA 29
 * 
 */

public class PlayerScreen extends JPanel {

	private GraphicsControl control;
	private JComboBox[] nameSelect;
	private JComboBox[] raceSelect;
	private JComboBox[] colorSelect;
	private int[] playerNamesIndex = { 0, 1, -1, -2 };
	private int[] playerColorsIndex = { 0, 1, -1, -2 };
	private boolean namesUnique, colorsUnique;
	private JPanel panel = this;

	private String[] names = { "BeanSter", "Caffina", "DoubleShot", "Expressa",
			"OrderUp" };

	private String[] races = { Player.Race.CARIBOUER.name,
			Player.Race.STARBUCKIAN.name, Player.Race.DUNKINER.name,
			Player.Race.BEANITE.name, Player.Race.GENERICAN.name };

	private String[] colors = { "Black", "Cherry Tea", "White Mocha",
			"Green Tea", "Lemonade" };

	/**
	 * Makes a screen with title, button to start the game, JPanels for each
	 * player and JComboBox for each player to choose name, race, color and
	 * difficulty
	 * 
	 * @param ctrl graphic control object 
	 * @param numPlayers number of players
	 */
	public PlayerScreen(GraphicsControl ctrl, final int numPlayers) {
		namesUnique = true;
		colorsUnique = true;
		control = ctrl;
		nameSelect = new JComboBox[numPlayers];
		raceSelect = new JComboBox[numPlayers];
		colorSelect = new JComboBox[numPlayers];

		setSize(new Dimension(900, 600));
		setBorder(new MatteBorder(4, 4, 4, 4, (Color) new Color(0, 0, 0)));
		setLayout(null);

		JLabel lblMainTitle = new JLabel("Coffee Mule");
		lblMainTitle.setBounds(175, 11, 563, 106);
		lblMainTitle.setForeground(new Color(165, 42, 42));
		lblMainTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblMainTitle.setFont(new Font("Batang", Font.BOLD, 75));
		add(lblMainTitle);

		JButton btnStart = new JButton("START GAME");
		btnStart.setBounds(296, 484, 482, 93);
		btnStart.setForeground(new Color(0, 0, 255));
		btnStart.setBorder(new BevelBorder(BevelBorder.RAISED, null, null,
				null, null));
		btnStart.setFont(new Font("Times New Roman", Font.PLAIN, 64));
		btnStart.addMouseListener(new Listener());
		add(btnStart);

		JLabel lblLogoSMALL = new JLabel("");
		lblLogoSMALL.setBounds(101, 484, 97, 93);
		lblLogoSMALL.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		lblLogoSMALL
				.setIcon(new ImageIcon("src/mulegame/Images/LogoSmall.JPG"));
		add(lblLogoSMALL);

		JPanel p1Border = new JPanel();
		p1Border.setBounds(63, 115, 365, 167);
		p1Border.setBorder(new TitledBorder(null, "Player 1",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(p1Border);
		p1Border.setLayout(null);

		JPanel player1 = new JPanel();
		player1.setBounds(6, 16, 353, 144);
		p1Border.add(player1);
		player1.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		player1.setToolTipText("Player 1");
		player1.setLayout(null);

		JLabel lblP1Name = new JLabel("Name");
		lblP1Name.setBounds(4, 5, 84, 47);
		player1.add(lblP1Name);
		lblP1Name.setHorizontalAlignment(SwingConstants.LEFT);
		lblP1Name.setForeground(Color.BLACK);
		lblP1Name.setFont(new Font("MV Boli", Font.BOLD, 29));

		JLabel lblP1Race = new JLabel("Race");
		lblP1Race.setBounds(4, 46, 68, 47);
		player1.add(lblP1Race);
		lblP1Race.setHorizontalAlignment(SwingConstants.LEFT);
		lblP1Race.setForeground(new Color(0, 0, 0));
		lblP1Race.setFont(new Font("MV Boli", Font.BOLD, 29));

		JLabel lblColor = new JLabel("Color");
		lblColor.setBounds(4, 93, 76, 47);
		player1.add(lblColor);
		lblColor.setHorizontalAlignment(SwingConstants.LEFT);
		lblColor.setForeground(Color.BLACK);
		lblColor.setFont(new Font("MV Boli", Font.BOLD, 29));

		final JComboBox comboBoxP1Name = new JComboBox();
		nameSelect[0] = comboBoxP1Name;
		comboBoxP1Name.setBounds(107, 11, 232, 35);
		player1.add(comboBoxP1Name);
		comboBoxP1Name.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null,
				null, null, null));
		comboBoxP1Name
				.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		comboBoxP1Name.setFont(new Font("MV Boli", Font.BOLD, 29));
		comboBoxP1Name.setModel(new DefaultComboBoxModel(names));
		comboBoxP1Name.setToolTipText("Choose a Name");
		comboBoxP1Name.setSelectedIndex(0);
		// comboBoxP1Name.setEditable(false);
		playerNamesIndex[0] = 0;
		comboBoxP1Name.addActionListener (new ActionListener () {
		    public void actionPerformed(ActionEvent e) {
				if (comboBoxP1Name.getSelectedIndex() == playerNamesIndex[1]
						|| comboBoxP1Name.getSelectedIndex() == playerNamesIndex[2]
						|| comboBoxP1Name.getSelectedIndex() == playerNamesIndex[3]) {
					namesUnique = false;
//					Logger.println(Boolean.toString(namesUnique));
					playerNamesIndex[0] = comboBoxP1Name.getSelectedIndex();
					JOptionPane.showMessageDialog(panel, "Names Must Be Unique!", "WARNING!",  JOptionPane.WARNING_MESSAGE);
					
					// comboBoxP1Name.setSelectedIndex(playerNamesIndex[0]);
					// comboBoxP1Name.revalidate();
					// comboBoxP1Name.repaint();
				} else {
					namesUnique = true;
					playerNamesIndex[0] = comboBoxP1Name.getSelectedIndex();
				}
			}
		});

		JComboBox comboBoxP1Race = new JComboBox();
		raceSelect[0] = comboBoxP1Race;
		comboBoxP1Race.setBounds(107, 54, 232, 35);
		player1.add(comboBoxP1Race);
		comboBoxP1Race.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null,
				null, null, null));
		comboBoxP1Race
				.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		comboBoxP1Race.setFont(new Font("MV Boli", Font.BOLD, 29));
		comboBoxP1Race.setModel(new DefaultComboBoxModel(races));
		comboBoxP1Race.setToolTipText("Choose a Race");
		comboBoxP1Race.setSelectedIndex(0);

		final JComboBox comboBoxP1Color = new JComboBox();
		colorSelect[0] = comboBoxP1Color;
		comboBoxP1Color.setBounds(107, 99, 232, 35);
		player1.add(comboBoxP1Color);
		comboBoxP1Color.setFont(new Font("MV Boli", Font.BOLD, 29));
		comboBoxP1Color.setModel(new DefaultComboBoxModel(colors));
		comboBoxP1Color
				.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		comboBoxP1Color.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,
				null, null, null, null));
		comboBoxP1Color.setToolTipText("Choose a Color");
		comboBoxP1Color.setSelectedIndex(0);
		playerColorsIndex[0] = 0;
		comboBoxP1Color.addActionListener (new ActionListener () {
		    public void actionPerformed(ActionEvent e) {
				if (comboBoxP1Color.getSelectedIndex() == playerColorsIndex[1]
						|| comboBoxP1Color.getSelectedIndex() == playerColorsIndex[2]
						|| comboBoxP1Color.getSelectedIndex() == playerColorsIndex[3]) {
					colorsUnique = false;
					playerColorsIndex[0] = comboBoxP1Color.getSelectedIndex();
					JOptionPane.showMessageDialog(panel, "Colors Must Be Unique!", "WARNING!",  JOptionPane.WARNING_MESSAGE);

					// comboBoxP1Name.setSelectedIndex(playerNamesIndex[0]);
					// comboBoxP1Name.revalidate();
					// comboBoxP1Name.repaint();
				} else {
					colorsUnique = true;
					playerColorsIndex[0] = comboBoxP1Color.getSelectedIndex();
				}
			}
		});

		JPanel p2Border = new JPanel();
		p2Border.setBounds(474, 115, 365, 167);
		p2Border.setBorder(new TitledBorder(null, "Player 2",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(p2Border);
		p2Border.setLayout(null);

		JPanel player2 = new JPanel();
		player2.setBounds(6, 16, 353, 144);
		p2Border.add(player2);
		player2.setLayout(null);
		player2.setToolTipText("Player 2");
		player2.setBorder(new LineBorder(new Color(0, 0, 0), 2));

		JLabel lblP2Name = new JLabel("Name");
		lblP2Name.setHorizontalAlignment(SwingConstants.LEFT);
		lblP2Name.setForeground(Color.BLACK);
		lblP2Name.setFont(new Font("MV Boli", Font.BOLD, 29));
		lblP2Name.setBounds(4, 5, 84, 47);
		player2.add(lblP2Name);

		JLabel lblP2Race = new JLabel("Race");
		lblP2Race.setHorizontalAlignment(SwingConstants.LEFT);
		lblP2Race.setForeground(Color.BLACK);
		lblP2Race.setFont(new Font("MV Boli", Font.BOLD, 29));
		lblP2Race.setBounds(4, 46, 68, 47);
		player2.add(lblP2Race);

		JLabel lblP2Color = new JLabel("Color");
		lblP2Color.setHorizontalAlignment(SwingConstants.LEFT);
		lblP2Color.setForeground(Color.BLACK);
		lblP2Color.setFont(new Font("MV Boli", Font.BOLD, 29));
		lblP2Color.setBounds(4, 93, 76, 47);
		player2.add(lblP2Color);

		final JComboBox comboBoxP2Name = new JComboBox();
		nameSelect[1] = comboBoxP2Name;
		comboBoxP2Name.setToolTipText("Choose a Name");
		comboBoxP2Name.setFont(new Font("MV Boli", Font.BOLD, 29));
		comboBoxP2Name.setModel(new DefaultComboBoxModel(names));
		comboBoxP2Name
				.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		comboBoxP2Name.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null,
				null, null, null));
		comboBoxP2Name.setBounds(107, 11, 232, 35);
		comboBoxP2Name.setSelectedIndex(1);
		playerNamesIndex[1] = 1;
		comboBoxP2Name.addActionListener (new ActionListener () {
		    public void actionPerformed(ActionEvent e) {
				if (comboBoxP2Name.getSelectedIndex() == playerNamesIndex[0]
						|| comboBoxP2Name.getSelectedIndex() == playerNamesIndex[2]
						|| comboBoxP2Name.getSelectedIndex() == playerNamesIndex[3]) {
					namesUnique = false;
					playerNamesIndex[1] = comboBoxP2Name.getSelectedIndex();
					JOptionPane.showMessageDialog(panel, "Names Must Be Unique!", "WARNING!",  JOptionPane.WARNING_MESSAGE);

					// comboBoxP1Name.setSelectedIndex(playerNamesIndex[0]);
					// comboBoxP1Name.revalidate();
					// comboBoxP1Name.repaint();
				} else {
					playerNamesIndex[1] = comboBoxP2Name.getSelectedIndex();
					namesUnique = true;
				}
			}
		});
		player2.add(comboBoxP2Name);

		JComboBox comboBoxP2Race = new JComboBox();
		raceSelect[1] = comboBoxP2Race;
		comboBoxP2Race.setToolTipText("Choose a Race");
		comboBoxP2Race.setFont(new Font("MV Boli", Font.BOLD, 29));
		comboBoxP2Race.setModel(new DefaultComboBoxModel(races));
		comboBoxP2Race
				.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		comboBoxP2Race.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null,
				null, null, null));
		comboBoxP2Race.setBounds(107, 54, 232, 35);
		comboBoxP2Race.setSelectedIndex(1);
		player2.add(comboBoxP2Race);

		final JComboBox comboBoxP2Color = new JComboBox();
		colorSelect[1] = comboBoxP2Color;
		comboBoxP2Color.setToolTipText("Choose a Color");
		comboBoxP2Color.setFont(new Font("MV Boli", Font.BOLD, 29));
		comboBoxP2Color.setModel(new DefaultComboBoxModel(colors));
		comboBoxP2Color
				.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		comboBoxP2Color.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,
				null, null, null, null));
		comboBoxP2Color.setBounds(107, 99, 232, 35);
		comboBoxP2Color.setSelectedIndex(1);
		playerColorsIndex[1] = 1;
		comboBoxP2Color.addActionListener (new ActionListener () {
		    public void actionPerformed(ActionEvent e) {
				if (comboBoxP2Color.getSelectedIndex() == playerColorsIndex[0]
						|| comboBoxP2Color.getSelectedIndex() == playerColorsIndex[2]
						|| comboBoxP2Color.getSelectedIndex() == playerColorsIndex[3]) {
					colorsUnique = false;
					playerColorsIndex[1] = comboBoxP2Color.getSelectedIndex();
					JOptionPane.showMessageDialog(panel, "Colors Must Be Unique!", "WARNING!",  JOptionPane.WARNING_MESSAGE);

					// comboBoxP1Name.setSelectedIndex(playerNamesIndex[0]);
					// comboBoxP1Name.revalidate();
					// comboBoxP1Name.repaint();
				} else {
					colorsUnique = true;
					playerColorsIndex[1] = comboBoxP2Color.getSelectedIndex();
				}
			}
		});
		player2.add(comboBoxP2Color);

		if (numPlayers > 2) {
			JPanel p3Border = new JPanel();
			p3Border.setBounds(63, 293, 365, 167);
			p3Border.setBorder(new TitledBorder(null, "Player 3",
					TitledBorder.LEADING, TitledBorder.TOP, null, null));
			add(p3Border);
			p3Border.setLayout(null);

			JPanel player3 = new JPanel();
			player3.setBounds(6, 16, 353, 144);
			p3Border.add(player3);
			player3.setLayout(null);
			player3.setToolTipText("Player 3");
			player3.setBorder(new LineBorder(new Color(0, 0, 0), 2));

			JLabel lblP3Name = new JLabel("Name");
			lblP3Name.setHorizontalAlignment(SwingConstants.LEFT);
			lblP3Name.setForeground(Color.BLACK);
			lblP3Name.setFont(new Font("MV Boli", Font.BOLD, 29));
			lblP3Name.setBounds(4, 5, 84, 47);
			player3.add(lblP3Name);

			JLabel lblP3Race = new JLabel("Race");
			lblP3Race.setHorizontalAlignment(SwingConstants.LEFT);
			lblP3Race.setForeground(Color.BLACK);
			lblP3Race.setFont(new Font("MV Boli", Font.BOLD, 29));
			lblP3Race.setBounds(4, 46, 68, 47);
			player3.add(lblP3Race);

			JLabel lblP3Color = new JLabel("Color");
			lblP3Color.setHorizontalAlignment(SwingConstants.LEFT);
			lblP3Color.setForeground(Color.BLACK);
			lblP3Color.setFont(new Font("MV Boli", Font.BOLD, 29));
			lblP3Color.setBounds(4, 93, 76, 47);
			player3.add(lblP3Color);

			final JComboBox comboBoxP3Name = new JComboBox();
			nameSelect[2] = comboBoxP3Name;
			comboBoxP3Name.setToolTipText("Choose a Name");
			comboBoxP3Name.setFont(new Font("MV Boli", Font.BOLD, 29));
			comboBoxP3Name.setModel(new DefaultComboBoxModel(names));
			comboBoxP3Name
					.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			comboBoxP3Name.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,
					null, null, null, null));
			comboBoxP3Name.setBounds(107, 11, 232, 35);
			player3.add(comboBoxP3Name);
			playerNamesIndex[2] = 2;
			comboBoxP3Name.addActionListener (new ActionListener () {
					    public void actionPerformed(ActionEvent e) {
							if (comboBoxP3Name.getSelectedIndex() == playerNamesIndex[0]
									|| comboBoxP3Name.getSelectedIndex() == playerNamesIndex[1]
									|| comboBoxP3Name.getSelectedIndex() == playerNamesIndex[3]) {
								namesUnique = false;
								playerNamesIndex[2] = comboBoxP3Name
										.getSelectedIndex();
								JOptionPane.showMessageDialog(panel, "Names Must Be Unique!", "WARNING!",  JOptionPane.WARNING_MESSAGE);

								// comboBoxP1Name.setSelectedIndex(playerNamesIndex[0]);
								// comboBoxP1Name.revalidate();
								// comboBoxP1Name.repaint();
							} else {
								playerNamesIndex[2] = comboBoxP3Name
										.getSelectedIndex();
								namesUnique = true;
							}
						}
					});
			comboBoxP3Name.setSelectedIndex(2);

			JComboBox comboBoxP3Race = new JComboBox();
			raceSelect[2] = comboBoxP3Race;
			comboBoxP3Race.setToolTipText("Choose a Race");
			comboBoxP3Race.setFont(new Font("MV Boli", Font.BOLD, 29));
			comboBoxP3Race.setModel(new DefaultComboBoxModel(races));
			comboBoxP3Race
					.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			comboBoxP3Race.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,
					null, null, null, null));
			comboBoxP3Race.setBounds(107, 54, 232, 35);
			comboBoxP3Race.setSelectedIndex(2);
			player3.add(comboBoxP3Race);

			final JComboBox comboBoxP3Color = new JComboBox();
			colorSelect[2] = comboBoxP3Color;
			comboBoxP3Color.setToolTipText("Choose a Color");
			comboBoxP3Color.setFont(new Font("MV Boli", Font.BOLD, 29));
			comboBoxP3Color.setModel(new DefaultComboBoxModel(colors));
			comboBoxP3Color
					.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			comboBoxP3Color.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,
					null, null, null, null));
			comboBoxP3Color.setBounds(107, 99, 232, 35);
			comboBoxP3Color.setSelectedIndex(2);
			playerColorsIndex[2] = 2;
			comboBoxP3Color.addActionListener (new ActionListener () {
					    public void actionPerformed(ActionEvent e) {
							if (comboBoxP3Color.getSelectedIndex() == playerColorsIndex[0]
									|| comboBoxP3Color.getSelectedIndex() == playerColorsIndex[1]
									|| comboBoxP3Color.getSelectedIndex() == playerColorsIndex[3]) {
								colorsUnique = false;
								playerColorsIndex[2] = comboBoxP3Color
										.getSelectedIndex();
								JOptionPane.showMessageDialog(panel, "Colors Must Be Unique!", "WARNING!",  JOptionPane.WARNING_MESSAGE);

								// comboBoxP1Name.setSelectedIndex(playerNamesIndex[0]);
								// comboBoxP1Name.revalidate();
								// comboBoxP1Name.repaint();
							} else {
								playerColorsIndex[2] = comboBoxP3Color
										.getSelectedIndex();
								colorsUnique = true;

							}
						}
					});
			player3.add(comboBoxP3Color);
		}

		if (numPlayers > 3) {
			JPanel p4Border = new JPanel();
			p4Border.setBounds(474, 293, 365, 167);
			p4Border.setBorder(new TitledBorder(null, "Player 4",
					TitledBorder.LEADING, TitledBorder.TOP, null, null));
			add(p4Border);
			p4Border.setLayout(null);

			JPanel player4 = new JPanel();
			player4.setBounds(6, 16, 353, 144);
			p4Border.add(player4);
			player4.setLayout(null);
			player4.setToolTipText("Player 4");
			player4.setBorder(new LineBorder(new Color(0, 0, 0), 2));

			JLabel lblP4Name = new JLabel("Name");
			lblP4Name.setHorizontalAlignment(SwingConstants.LEFT);
			lblP4Name.setForeground(Color.BLACK);
			lblP4Name.setFont(new Font("MV Boli", Font.BOLD, 29));
			lblP4Name.setBounds(4, 5, 84, 47);
			player4.add(lblP4Name);

			JLabel lblP4Race = new JLabel("Race");
			lblP4Race.setHorizontalAlignment(SwingConstants.LEFT);
			lblP4Race.setForeground(Color.BLACK);
			lblP4Race.setFont(new Font("MV Boli", Font.BOLD, 29));
			lblP4Race.setBounds(4, 46, 68, 47);
			player4.add(lblP4Race);

			JLabel lblP4Color = new JLabel("Color");
			lblP4Color.setHorizontalAlignment(SwingConstants.LEFT);
			lblP4Color.setForeground(Color.BLACK);
			lblP4Color.setFont(new Font("MV Boli", Font.BOLD, 29));
			lblP4Color.setBounds(4, 93, 76, 47);
			player4.add(lblP4Color);

			final JComboBox comboBoxP4Name = new JComboBox();
			nameSelect[3] = comboBoxP4Name;
			comboBoxP4Name.setToolTipText("Choose a Name");
			comboBoxP4Name.setFont(new Font("MV Boli", Font.BOLD, 29));
			comboBoxP4Name.setModel(new DefaultComboBoxModel(names));
			comboBoxP4Name
					.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			comboBoxP4Name.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,
					null, null, null, null));
			comboBoxP4Name.setBounds(107, 11, 232, 35);
			comboBoxP4Name.setSelectedIndex(3);
			playerNamesIndex[3] = 3;
			comboBoxP4Name.addActionListener (new ActionListener () {
			    public void actionPerformed(ActionEvent e) {
							if (comboBoxP4Name.getSelectedIndex() == playerNamesIndex[0]
									|| comboBoxP4Name.getSelectedIndex() == playerNamesIndex[1]
									|| comboBoxP4Name.getSelectedIndex() == playerNamesIndex[2]) {
								namesUnique = false;
								playerNamesIndex[3] = comboBoxP4Name
										.getSelectedIndex();
								JOptionPane.showMessageDialog(panel, "Names Must Be Unique!", "WARNING!",  JOptionPane.WARNING_MESSAGE);

								// comboBoxP1Name.setSelectedIndex(playerNamesIndex[0]);
								// comboBoxP1Name.revalidate();
								// comboBoxP1Name.repaint();
							} else {
								playerNamesIndex[3] = comboBoxP4Name
										.getSelectedIndex();
								namesUnique = true;
							}
						}
					});
			player4.add(comboBoxP4Name);

			JComboBox comboBoxP4Race = new JComboBox();
			raceSelect[3] = comboBoxP4Race;
			comboBoxP4Race.setToolTipText("Choose a Race");
			comboBoxP4Race.setFont(new Font("MV Boli", Font.BOLD, 29));
			comboBoxP4Race.setModel(new DefaultComboBoxModel(races));
			comboBoxP4Race
					.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			comboBoxP4Race.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,
					null, null, null, null));
			comboBoxP4Race.setBounds(107, 54, 232, 35);
			comboBoxP4Race.setSelectedIndex(3);
			player4.add(comboBoxP4Race);

			final JComboBox comboBoxP4Color = new JComboBox();
			colorSelect[3] = comboBoxP4Color;
			comboBoxP4Color.setToolTipText("Choose a Color");
			comboBoxP4Color.setFont(new Font("MV Boli", Font.BOLD, 29));
			comboBoxP4Color.setModel(new DefaultComboBoxModel(colors));
			comboBoxP4Color
					.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			comboBoxP4Color.setBorder(new SoftBevelBorder(BevelBorder.LOWERED,
					null, null, null, null));
			comboBoxP4Color.setBounds(107, 99, 232, 35);
			comboBoxP4Color.setSelectedIndex(3);
			playerColorsIndex[3] = 3;
			comboBoxP4Color.addActionListener (new ActionListener () {
			    public void actionPerformed(ActionEvent e) {
							if (comboBoxP4Color.getSelectedIndex() == playerColorsIndex[0]
									|| comboBoxP4Color.getSelectedIndex() == playerColorsIndex[1]
									|| comboBoxP4Color.getSelectedIndex() == playerColorsIndex[2]) {
								colorsUnique = false;
								playerColorsIndex[3] = comboBoxP4Color
										.getSelectedIndex();
								JOptionPane.showMessageDialog(panel, "Colors Must Be Unique!", "WARNING!",  JOptionPane.WARNING_MESSAGE);

								// comboBoxP1Name.setSelectedIndex(playerNamesIndex[0]);
								// comboBoxP1Name.revalidate();
								// comboBoxP1Name.repaint();
							} else {
								playerColorsIndex[3] = comboBoxP4Color
										.getSelectedIndex();
								colorsUnique = true;

							}
						}
					});
			player4.add(comboBoxP4Color);
		}
	}

	/**
	 * Controls the choices, gives dropdown menu for race and color
	 * 
	 * @author AREA 29
	 * 
	 */
	private class Listener extends MouseAdapter {

		@Override
		public void mousePressed(MouseEvent arg0) {
			if(!namesUnique && !colorsUnique){
				JOptionPane.showMessageDialog(panel, "Names and Colors Must Be Unique!", "WARNING!",  JOptionPane.WARNING_MESSAGE);

			}
			else if (!namesUnique) {
				JOptionPane.showMessageDialog(panel, "Names Must Be Unique!", "WARNING!",  JOptionPane.WARNING_MESSAGE);
			} else if (!colorsUnique) {
				JOptionPane.showMessageDialog(panel, "Colors Must Be Unique!", "WARNING!",  JOptionPane.WARNING_MESSAGE);
			} else {
				Logger.println(Boolean.toString(namesUnique));
				Logger.println(Boolean.toString(colorsUnique));

				
				String[] currNames = new String[nameSelect.length];
				int i = 0;
				for (JComboBox box : nameSelect) {
					currNames[i++] = names[box.getSelectedIndex()];
				}

				Player.Race[] races = new Player.Race[raceSelect.length];
				Player.Race currRace = null;
				i = 0;
				for (JComboBox box : raceSelect) {
					switch (box.getSelectedIndex()) {
					case 0:
						currRace = Player.Race.CARIBOUER;
						break;
					case 1:
						currRace = Player.Race.STARBUCKIAN;
						break;
					case 2:
						currRace = Player.Race.DUNKINER;
						break;
					case 3:
						currRace = Player.Race.BEANITE;
						break;
					case 4:
						currRace = Player.Race.GENERICAN;
						break;
					}
					races[i++] = currRace;
				}

				Color[] colors = new Color[colorSelect.length];
				Color currCol = null;
				i = 0;
				for (JComboBox box : colorSelect) {
					switch (box.getSelectedIndex()) {
					case 0:
						currCol = Color.BLACK;
						break;
					case 1:
						currCol = Color.RED;
						break;
					case 2:
						currCol = Color.WHITE;
						break;
					case 3:
						currCol = Color.GREEN;
						break;
					case 4:
						currCol = Color.YELLOW;
						break;
					}
					colors[i++] = currCol;

				}

				// TODO check to see if the same name is used, or the same color
				// is used
				doClickSound(); 
				control.finishPlayerScreen(currNames, races, colors);
			}
		}
	}
   /**
    * plays sound file for clicking actions
    */
    public void doClickSound() {
    	String soundName = "src/mulegame/Sounds/applause.wav";    
    	AudioInputStream audioInputStream = null;
		try {
			audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
		} catch (UnsupportedAudioFileException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	Clip clip = null;
		try {
			clip = AudioSystem.getClip();
		} catch (LineUnavailableException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	try {
			clip.open(audioInputStream);
		} catch (LineUnavailableException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	clip.start();
    }
}
